//
//  DatePickerCell.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/5.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit

protocol HDDatePickerCellDelegate: NSObjectProtocol{
    func finishDatePick(_ date: Date)
}

class HDDatePickerCell: HDFounctionBaseCell , HDInputViewStateProtocol {
    //MARK: - property
    weak var delegate:HDDatePickerCellDelegate?
    var inputState:inputViewState = inputViewState.disabled
    var state:inputViewState?{
        get{
            return inputState
        }
        set{
            inputState = newValue!
            switch inputState { //改变状态
            case .activated:
                self.isUserInteractionEnabled = true
                self.infoView.becomeFirstResponder()
//                self.infoView.layer.borderColor = UIColor.hexStringToUIColor("#0ebfb1").CGColor
//                self.infoView.layer.borderWidth = 1.5
            case .disabled:
                self.isUserInteractionEnabled = false
//                self.infoView.layer.borderColor = UIColor.hexStringToUIColor("#bbbbbb").CGColor
//                self.infoView.layer.borderWidth = 1.5
            case .wrong:
                self.isUserInteractionEnabled = true
//                self.infoView.layer.borderColor = UIColor.redColor().CGColor
//                self.infoView.layer.borderWidth = 1.5
            }
            
        }
    }

    var date = Date()
    
    override var info: [String : String]?{
         didSet{
            self.titleView.text = "生产日期"
        }
    }
    
    lazy fileprivate var infoView: UITextField = { //日期展示cell
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 20))
        let textfield = UITextField()
        textfield.leftView = leftView
//        textfield.leftView?.backgroundColor = UIColor.redColor()
        textfield.leftViewMode = UITextFieldViewMode.always
        let datepicker = UIDatePicker()  //添加pickerview日期选择器
        datepicker.backgroundColor = UIColor.red
        datepicker.locale = Locale(identifier: "zh-Hans")
        datepicker.datePickerMode = UIDatePickerMode.date
        datepicker.backgroundColor = UIColor.white
        textfield.inputView = datepicker
        let formater = DateFormatter()//初始化日期为系统日期
        formater.dateFormat = "yyyy/MM/dd"
//        textfield.backgroundColor = UIColor.redColor()
        textfield.text = formater.string(from: Date())
        textfield.textColor = UIColor.hexStringToUIColor("#666666")
        textfield.tintColor = UIColor.hexStringToUIColor("#00acff")
        textfield.font = UIFont.systemFont(ofSize: self.getFont())
        textfield.textAlignment = .left
        return textfield
    }()
    lazy fileprivate var datePickerButton:UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "日期icon"), for: UIControlState())
        button.setImage(UIImage(named: "日期icon-hover"), for: UIControlState.highlighted)
        return button
    }()

    
    //MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "DatePickerCell")
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUI()
    }
    
    required init() {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "DatePickerCell")
        self.setUpUI()
    }
    //MARK: - public
    override func clearContent() {
        let formater = DateFormatter()//初始化日期为系统日期
        formater.dateFormat = "yyyy/MM/dd"
        self.infoView.text = formater.string(from: Date())
    }
    //MARK: - private
    func setUpUI(){
        self.state = inputViewState.disabled
        self.accessoryType = UITableViewCellAccessoryType.none
        self.addSubview(self.infoView)
        self.addSubview(self.datePickerButton)
        infoView.snp.makeConstraints { (make) in
            make.leading.equalTo(self).offset(inputViewToCellSpace-10)
            make.top.equalTo(self).offset(FTCellTopSpace)
            make.bottom.equalTo(self).offset(-FTCellTopSpace)
            make.trailing.equalTo(self).offset(-FTCellHorizontalSpace)
        }
        datePickerButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(self).offset(-15)
            make.centerY.equalTo(self)
        }
        self.titleView.snp.remakeConstraints { (make) in
            make.trailing.equalTo(self.contentView).offset(95 - KscreenWidth)
            make.centerY.equalTo(self)
            make.width.equalTo(80)
            
        }
        self.datePickerButton.addTarget(self, action: #selector(HDDatePickerCell.clickDatePickerButton), for: UIControlEvents.touchUpInside)
        let pickerview:UIDatePicker = self.infoView.inputView as! UIDatePicker
        pickerview.addTarget(self, action: #selector(HDDatePickerCell.pickerChanged(_:)), for: UIControlEvents.valueChanged)
        let donebutton = UIButton()
        donebutton.setTitle("确定", for: UIControlState())
        donebutton.setTitleColor(UIColor.black, for: UIControlState())
//        donebutton.backgroundColor = UIColor.blackColor()
        donebutton.addTarget(self, action:  #selector(HDDatePickerCell.pickerDone), for: UIControlEvents.touchUpInside)
        donebutton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        
        let canclebutton = UIButton()
        canclebutton.setTitle("取消", for: UIControlState())
        canclebutton.setTitleColor(UIColor.black, for: UIControlState())
//        canclebutton.backgroundColor = UIColor.blackColor()
        canclebutton.addTarget(self, action:  #selector(HDDatePickerCell.pickerCancel), for: UIControlEvents.touchUpInside)
        canclebutton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        
        let label = UILabel()
        label.text = "商品生产日期"
//        label.backgroundColor = UIColor.blueColor()
        
        
        let accview = UIView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: 40))
        accview.backgroundColor = UIColor.hexStringToUIColor("#e5e5e5")
        accview.addSubview(donebutton)
        accview.addSubview(canclebutton)
        accview.addSubview(label)
        
        donebutton.snp.makeConstraints { (make) in
            make.trailing.equalTo(accview).offset(-10)
            make.centerY.equalTo(accview)
            make.height.equalTo(30)
        }
        canclebutton.snp.makeConstraints { (make) in
            make.leading.equalTo(accview).offset(10)
            make.centerY.equalTo(accview)
            make.height.equalTo(30)
        }
        
        label.snp.makeConstraints { (make) in
            make.centerX.equalTo(accview)
            make.centerY.equalTo(accview)
            make.height.equalTo(30)
        }
        self.infoView.inputAccessoryView = accview
    }
    @objc fileprivate func clickDatePickerButton(){
        self.infoView.becomeFirstResponder()
    }
    @objc func pickerDone(){
        if self.delegate != nil {
            self.delegate?.finishDatePick(date)
            self.infoView.resignFirstResponder()
        }
    }
    @objc func pickerCancel(){
        self.infoView.resignFirstResponder()
    }

    @objc fileprivate func pickerChanged(_ datePicker: UIDatePicker){
        let formater = DateFormatter()
        formater.dateFormat = "yyyy-MM-dd"
        date = datePicker.date
        self.infoView.text = formater.string(from: datePicker.date)
    }
    
    //MARK: - delegate
    func finishInput(_ text: String?) {
        
    }
   

}
